//
//  LXAddressManagerViewController.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/16.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//.......................................地址管理.......................................//
//............................................................................................//

#import "LXAddressManagerViewController.h"
#import "LXEditAddressViewCell.h"
#import "LXAddNewAddressViewController.h"


static NSString *const LXEditAddressViewCellID = @"LXEditAddressViewCell";

@interface LXAddressManagerViewController ()<UITableViewDataSource,UITableViewDelegate,LXEditAddressViewCellDelegate>

@property (nonatomic,weak) UITableView *tableView;

@property(nonatomic,strong)NSMutableArray * addressArray;

@property(nonatomic,assign)NSInteger sections;
@end

@implementation LXAddressManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"地址管理";
    self.sections = 10;
    self.addressArray = [NSMutableArray array];
    self.sections = self.addressArray==nil?0:self.addressArray.count;
    [self setUpTableView];
    [self xw_registerBackInteractiveTransitionWithDirection:XWInteractiveTransitionGestureDirectionRight transitonBlock:^(CGPoint startPoint) {
        [self.navigationController popViewControllerAnimated:YES];
    } edgeSpacing:50];
}

- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0);
    tableView.rowHeight = MineRatio(60);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self.tableView registerClass:[LXEditAddressViewCell class] forCellReuseIdentifier:LXEditAddressViewCellID];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
    [[LXDataManager manager] getAddrListWithComplete:^(BOOL success, id res) {
        if(success){
            [self.addressArray removeAllObjects];
            [self.addressArray addObjectsFromArray:res];
            self.sections = self.addressArray==nil?0:self.addressArray.count;
            [self.tableView reloadData];
        }else{
            [self.view alertWithTitle:@"提示" mesege:@"信息有误请检查" buttons:@[@"好的"] handle:^(NSInteger buttonIndex) {
                [self.view dismissAlert];
            }];
        }
    }];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"收货地址";
    UIButton * addNewAddress = [UIButton buttonWithType:UIButtonTypeCustom];
    [addNewAddress setTitle:@"新增" forState:UIControlStateNormal];
    [addNewAddress setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addNewAddress.frame = CGRectMake(0, 0, 50, 30);
//    [addNewAddress setImage:[UIImage imageNamed:@"nav_return"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addNewAddress];
    [addNewAddress addTarget:self action:@selector(addNewAddress:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)backButtonClick:(UIButton*)aButton{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)addNewAddress:(UIButton*)button{
    LXAddNewAddressViewController *addNewAddressVc = [[LXAddNewAddressViewController alloc] init];
    [self.navigationController pushViewController:addNewAddressVc animated:YES];
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXEditAddressViewCell *editAddressCell = [tableView dequeueReusableCellWithIdentifier:LXEditAddressViewCellID];
      AddressModel * address = self.addressArray[indexPath.section];
        editAddressCell.nameLabel.text = address.name;
        editAddressCell.phoneLabel.text = address.mobile;
        editAddressCell.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",address.province,address.city,address.district];
        editAddressCell.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, editAddressCell.cellHeight)];
        [editAddressCell.backgroundImageView addRadius:10];
    editAddressCell.delegate = self;
        return editAddressCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  return MineRatio(144);

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LXEditAddressViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSMutableDictionary * mdic = [NSMutableDictionary dictionary];
    [mdic setObject:cell.phoneLabel.text forKey:@"ship_mobile"];
    [mdic setObject:cell.addressLabel.text forKey:@"ship_addr"];
    [mdic setObject:cell.nameLabel.text forKey:@"ship_name"];
    if(self.delegate&&[self.delegate respondsToSelector:@selector(DidSelectedContect:)]){
        [self.delegate DidSelectedContect:mdic];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



-(void)LXEditAddressViewCell:(LXEditAddressViewCell *)cell didClickEditButton:(UIButton *)button{
    
}

-(void)LXEditAddressViewCell:(LXEditAddressViewCell *)cell didClickDeleteButton:(UIButton *)button{
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    AddressModel * address = self.addressArray[indexPath.section];
    [[LXDataManager manager] delAddress:address complete:^(BOOL success, id res) {
        [self.addressArray removeObject:address];
        self.sections = self.addressArray.count;
        [self.tableView reloadData];
    }];
}

@end
